/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import controller.SessionController;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import management.RequestManager;
import model.Charter;
import model.Issue;
import model.Session;
import threads.ProgressThread;

/**
 *
 * @author heitor
 */
public class SessionPanelForm extends javax.swing.JPanel {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new SessionPanelForm(new Session(new Charter("Teste"))));
        f.pack();
        f.setVisible(true);
    }
    private final RequestManager rm;
    private final Session session;
    private final int time;
    private ProgressThread progressThread;

    /**
     * Creates new form SessionPanelForm
     */
    public SessionPanelForm(Session session) {
        this.session = session;
        rm = new RequestManager(new JFrame());
        time = Integer.valueOf(session.getDuration());
        initComponents();
        manuallyInitComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        charterPanel1 = new gui.CharterPanel();
        issuesLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        issuesTextArea = new javax.swing.JTextArea();
        commentsLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        commentsTA = new javax.swing.JTextArea();
        newIssue = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        saveButton = new javax.swing.JButton();
        remainingTimeBar = new javax.swing.JProgressBar();
        remainingTimeLabel = new javax.swing.JLabel();
        reportButton = new javax.swing.JButton();
        pauseToggleButton = new javax.swing.JToggleButton();

        issuesLabel.setText("Issues");

        issuesTextArea.setColumns(20);
        issuesTextArea.setRows(5);
        jScrollPane1.setViewportView(issuesTextArea);

        commentsLabel.setText("Comments");

        commentsTA.setColumns(20);
        commentsTA.setRows(5);
        jScrollPane2.setViewportView(commentsTA);

        newIssue.setText("Add Issue");

        saveButton.setText("jButton1");

        remainingTimeLabel.setText("Remaining time:");

        reportButton.setText("jButton1");
        reportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportButtonActionPerformed(evt);
            }
        });

        pauseToggleButton.setText("Pause");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(charterPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reportButton))
                    .addComponent(remainingTimeLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(pauseToggleButton))
                    .addComponent(remainingTimeBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(commentsLabel)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(issuesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(newIssue)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(newIssue)
                            .addComponent(issuesLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(charterPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(saveButton)
                            .addComponent(reportButton))))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(commentsLabel)
                    .addComponent(remainingTimeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(remainingTimeBar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(pauseToggleButton)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportButtonActionPerformed
        setValuesToAtribute();
        String nameSessionSaved = JOptionPane.showInputDialog(null, rm.loadProperty(EntryPoint.getLanguage() + "_Name_session_saved"));
        SessionController.getInstance().generateReport(session, nameSessionSaved);
    }//GEN-LAST:event_reportButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gui.CharterPanel charterPanel1;
    private javax.swing.JLabel commentsLabel;
    private javax.swing.JTextArea commentsTA;
    private javax.swing.JLabel issuesLabel;
    private javax.swing.JTextArea issuesTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton newIssue;
    private javax.swing.JToggleButton pauseToggleButton;
    private javax.swing.JProgressBar remainingTimeBar;
    private javax.swing.JLabel remainingTimeLabel;
    private javax.swing.JButton reportButton;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

    private void manuallyInitComponents() {
        
        commentsLabel.setText(rm.loadProperty(EntryPoint.getLanguage() + "_Comments"));
        charterPanel1.setCharter(session.getCharter());
        issuesLabel.setText(rm.loadProperty(EntryPoint.getLanguage() + "_Issues_title"));
        newIssue.setText(rm.loadProperty(EntryPoint.getLanguage() + "_New_Issue"));
        saveButton.setText(rm.loadProperty(EntryPoint.getLanguage() + "_Save"));
        reportButton.setText(rm.loadProperty(EntryPoint.getLanguage() + "_Generate_report_button"));
        remainingTimeBar.setMaximum(time);
        remainingTimeBar.setMinimum(0);
        progressThread = new ProgressThread(remainingTimeBar, time, session);
        progressThread.start();
        addActions();

    }
    private void setValuesToAtribute(){
        session.setComments(commentsTA.getText());
    }

    private void addActions() {
        newIssue.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Issue iss = new Issue();
                NewIssueScreen nis = new NewIssueScreen(iss, issuesTextArea, session);
            }
        });
        
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setValuesToAtribute();
                try {
                    String filename = JOptionPane.showInputDialog(null, rm.loadProperty(EntryPoint.getLanguage() + "_Path_filename_question"),
                            rm.loadProperty(EntryPoint.getLanguage() + "_Path_filename_question"), JOptionPane.PLAIN_MESSAGE);
                    filename+=".xml";
                    
                    if (!filename.trim().equalsIgnoreCase(".xml") && !filename.trim().equalsIgnoreCase("null.xml")) {
                        SessionController.getInstance().saveSession(session, filename);
                        JOptionPane.showMessageDialog(null, rm.loadProperty(EntryPoint.getLanguage() + "_Session_saved"));
                    }
                    
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(null,  rm.loadProperty(EntryPoint.getLanguage() + "_Error_file_msg"),
                             rm.loadProperty(EntryPoint.getLanguage() + "_Error"), JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        
        pauseToggleButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (pauseToggleButton.isSelected()) {
                    progressThread.halt(remainingTimeBar.getValue());
                    pauseToggleButton.setText("Resume");
                } else {
                    progressThread.restore(remainingTimeBar.getValue());
                    pauseToggleButton.setText("Pause");
                }
            }
        });
    }
}
